import os

with open("backend/risk/models.py",'r') as f:
    # l = ["BuhegehouchuliGuoshengshichou","BuhegehouchuliWaidi"]
    l = [l[6:l.find('(')] for l in f.readlines() if l.startswith("class ")]

serial_template = '''
class { abab }Serializer(CustomModelSerializer):
    """
    序列化器
    """

    class Meta:
        model = { abab }
        fields = "__all__"


class { abab }CreateUpdateSerializer(CustomModelSerializer):
    """
    创建/更新时的序列化器
    """

    class Meta:
        model = { abab }
        fields = '__all__'

'''

view_template = '''# Create your views here.
from risk.models import *
from risk.serializers import *
from dvadmin.utils.viewset import CustomModelViewSet


class { abab }ViewSet(CustomModelViewSet):
    """
    list:查询
    create:新增
    update:修改
    retrieve:单例
    destroy:删除
    """
    queryset = { abab }.objects.all()
    serializer_class = { abab }Serializer
    create_serializer_class = { abab }CreateUpdateSerializer
    update_serializer_class = { abab }CreateUpdateSerializer
    filter_fields = []
    search_fields = []
'''


app_path = 'backend/risk'

if False:
    with open(f'{app_path}/serializers.py', 'a') as f:
        for m in l:
            f.write(serial_template.replace('{ abab }',m))


folder_path = f'{app_path}/views'

# 获取文件夹下的所有文件和子文件夹
files = os.listdir(folder_path)

if False:

    for m in l:
        view_path = os.path.join(folder_path, f"{m.lower()}.py")
        if not os.path.exists(view_path):
            with open(view_path, 'w') as f:
                f.write(view_template.replace('{ abab }',m)) 


if True:
    view_init = os.path.join(folder_path, f"__init__.py")
    view_init_template = 'from .{ abab } import { Abab }ViewSet\n'
    with open(view_init, 'a') as f:
        for m in l:
            f.write(view_init_template.replace('{ Abab }',m).replace('{ abab }',m.lower()))



# l = ['事件标题', '日期','具体产品','危害源','伤害类型','链接']

# template = """            {
#                 title: "{ abab }",
#                 key: "{ abab }",
#                 sortable: true,
#                 treeNode: true,

#                 type: "input",
#                 search: {
#                   component: {
#                       props: {
#                           clearable: true
#                       }
#                   }
#                 },
#                 form: {
#                     editDisabled: true,
#                     rules: [
#                         // 表单校验规则
#                         { required: true, message: "{ abab }必填" }
#                     ],
#                     component: {
#                         props: {
#                             clearable: true
#                         },
#                         placeholder: "请输入{ abab }"
#                     },
#                     itemProps: {
#                         class: { yxtInput: true }
#                     }
#                 }
#             },
# """

# for i in l:
#     print(template.replace('{ abab }',i))